package com.wch.serverdemo.jdbc;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import test.RedisDaoImplTest;

/**
 * 
 * @author WanChengHe
 * 模拟多数据源切换
 *
 */
public class DbContextHoldrer {

	private static final ThreadLocal<String> sContextHolder = new ThreadLocal<String>();
	
	private static Logger logger = LoggerFactory.getLogger(DbContextHoldrer.class);
	private static final String DATA_SOURCE_MINE = "dataSourceMine";
	private static final String DATA_SOURCE_JX = "dataSourceJX";
	
	public static void setDbType(String dbType){
		sContextHolder.set(dbType);
	}
	
	public static String getDbType(){
		return sContextHolder.get();
	}
	
	/**
	 * 根据区域回去匹配相应的库
	 * @param sharingId
	 */
	public static void setDbTypeByShardingId(String sharingId){
		if("791".equals(sharingId)){
			setDbType(DATA_SOURCE_JX);
			logger.debug("Master To 134.225.80.53");
		}else if("792".equals(sharingId)){
			setDbType(DATA_SOURCE_MINE);
			logger.debug("Master To 127.0.0.1");
		}
	}
}
